//
//  TGCustomCalloutView.m
//  GroupBuy
//
//  Created by wwj on 15/11/19.
//  Copyright © 2015年 wwj. All rights reserved.
//

#import "TGCustomCalloutView.h"
#import "UIImageView+WebCache.h"
#import "UIImage+Extend.h"
#import "TGBusinessModel.h"
#import <CoreLocation/CoreLocation.h>
#import "TGDealModel.h"

#define kCalloutWidth       255.0
#define kCalloutHeight      90.0

typedef void (^Block)();

@interface TGCustomCalloutView ()
@property (nonatomic, weak) IBOutlet UIImageView *portraitView;
@property (nonatomic, weak) IBOutlet UILabel *subtitleLabel;
@property (nonatomic, weak) IBOutlet UILabel *titleLabel;

@end

@implementation TGCustomCalloutView

+(instancetype)customCalloutView{
    return [[NSBundle mainBundle] loadNibNamed:@"TGCustomCalloutView" owner:nil options:nil].lastObject;
}

-(void)awakeFromNib{
    _portraitView.userInteractionEnabled = YES;
    [_portraitView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClick)]];
}

-(void)onClick{

}



-(void)setBmodel:(TGBusinessModel *)bmodel{
    _bmodel = bmodel;
    [_portraitView sd_setImageWithURL:[NSURL URLWithString:bmodel.image] placeholderImage:[UIImage imageNamed:@"ic_map_placeholder"]];
    
    _titleLabel.text = bmodel.name;
    
    CLLocation *location = [[CLLocation alloc] initWithLatitude:_bmodel.latitude longitude:_bmodel.longitude];
    [[[CLGeocoder alloc] init] reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        CLPlacemark *placemark = placemarks.firstObject;
        NSDictionary *dict = placemark.addressDictionary;
        NSMutableString *locationStr =[NSMutableString string];
        [locationStr appendString:@"地址: "];
        if (dict[@"Thoroughfare"]) {
            [locationStr appendString: dict[@"Thoroughfare"]];
            [locationStr appendString: @" "];
        }
        
        
        if (dict[@"SubThoroughfare"]) {
            [locationStr appendString: dict[@"SubThoroughfare"]];
        }
        _subtitleLabel.text = locationStr;

    }];
}



-(void)drawRect:(CGRect)rect{
    [[UIImage resizedImage:@"bg_map_merchant_pop"] drawInRect:rect];
}

@end
